{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit Modifiers;

interface

uses
  Classes, SysUtils, MiscUtils, TestCore;

type
  TModifierRegistry = class
  private type
    TEntryList = TGenericList<TModifierClass>;
  private
    FEntries: TEntryList;
    function GetCount: Integer;
    procedure CheckIndex(Index: Integer);
  public
    constructor Create;
    destructor Destroy; override;
    procedure Add(ModifierClass: TModifierClass);
    function Get(Index: Integer): TModifierClass;
    function List: TModifierClassArray;

    property Count: Integer read GetCount;
  end;

function ModifierRegistry: TModifierRegistry;

implementation

var
  Registry: TModifierRegistry;

function ModifierRegistry: TModifierRegistry;
begin
  Result := Registry;
end;

{ TModifierRegistry }

function TModifierRegistry.GetCount: Integer;
begin
  Result := FEntries.Count;
end;

procedure TModifierRegistry.CheckIndex(Index: Integer);
begin
  Assert( Index >= 0 );
  Assert( Index < FEntries.Count );
end;

constructor TModifierRegistry.Create;
begin
  inherited;
  FEntries := TEntryList.Create;
end;

destructor TModifierRegistry.Destroy;
begin
  FreeAndNil(FEntries);
  inherited;
end;

procedure TModifierRegistry.Add(ModifierClass: TModifierClass);
begin
  FEntries.Add(ModifierClass);
end;

function TModifierRegistry.Get(Index: Integer): TModifierClass;
begin
  CheckIndex(Index);
  Result := FEntries[Index];
end;

function TModifierRegistry.List: TModifierClassArray;
var
  i: Integer;
begin
  SetLength(Result, Count);
  for i := 0 to High(Result) do
    Result[i] := FEntries[i];
end;

initialization

  Registry := TModifierRegistry.Create;

finalization

  FreeAndNil(Registry);

end.

